package org.focusj.tip

abstract class Tree
case class Sum(l: Tree, r: Tree) extends Tree
case class Var(n: String) extends Tree
case class Const(v: Int) extends Tree

case class Person(id: Long, name: String, gender: String)

object CaseClassTest {
    type Environment = String => Int

    def eval(t: Tree, env: Environment): Int = t match {
        case Sum(l, r) => eval(l, env) + eval(r, env)
        case Var(n) => env(n)
        case Const(v) => v
    }

    def changeAll(person: Person) = {
        val Person(id, name, gender) = person
        Person(id + 1, name + "-bak", gender + "-gender")
    }

    def main(args: Array[String]) {
        val env: Environment = { case "a" => 5 }
        val a = eval(Var("a"), env)
        println(a)
        println(changeAll(Person(1, "wang", "kang")))
    }
}